package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


@RestController
@RequestMapping("/role")
//@EnableGlobalMethodSecurity
//extends WebSecurityConfigurerAdapter
@PreAuthorize("principal.username.equals('admin')")
public class RoleController  {


    @Reference(version = "1.0.0")
    private RoleService roleService;

    @PostMapping("getRole")
    public Result save(@RequestBody Role role){
        return roleService.save(role);
    }

    @PostMapping("/findPage")
    public Result findPage( @RequestBody QueryPageBean queryPageBean) {

        if (queryPageBean.getQueryString() != null && queryPageBean.getCurrentPage() != 1) {
            queryPageBean.setCurrentPage(1);
        }
        return roleService.findPage(queryPageBean);
    }

    //查询角色的权限
    @PostMapping("/findAll")
    public Result findAll() {
        List<Role> roleList = roleService.findAll();
        return Result.success(roleList);
    }

    @PostMapping("/add")
    //ROLE_ADD
    //@PreAuthorize("hasAnyAuthority('ROLE_ADD_0')")
    //登录用户名是admin的用户才可以访问
    @Secured("ROLE_ADMIN")
    @PreAuthorize("principal.username.equals('admin')")
    //@Secured("ROLE_ADMIN")
    public Result add(@RequestBody Role role) {
     /*   String keyword = role.getKeyword();
        //获取当前的认证信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //生成当前是所有的授权
        List<GrantedAuthority> updatedAuthorities= new ArrayList<>(authentication.getAuthorities());
        //添加授权
        updatedAuthorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        //生成新的认证消息
        Authentication newAuth=new UsernamePasswordAuthenticationToken(authentication.getPrincipal(),authentication.getCredentials(),updatedAuthorities);
        //重置认证消息
        SecurityContextHolder.getContext().setAuthentication(newAuth);
        System.out.println(authentication);*/
        return roleService.add(role);
    }

    @GetMapping("/findById")
    public Result findById(Integer id) {
        return roleService.findById(id);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Role role) {
        return roleService.update(role);
    }

    @GetMapping("/delete")
    @PreAuthorize("principal.username.equals('admin1')")
    public Result delete(Integer id) {
        return roleService.delete(id);
    }
}
